/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2022-2022. All rights reserved.
 *
 */

import * as fse from 'fs-extra';

import { KeyStoreHelper } from '../utils/keystore-utils';

import { TaskNames } from './common/task-names';
import { TargetTaskService } from './service/target-task-service.js';
import { OhosHapTask } from './task/ohos-hap-task';
import CommonTask = TaskNames.CommonTask;
import { LegacyPreBuild } from './legacy-tasks/legacy-pre-build.js';

/**
 * 校验签名配置
 *
 * @since 2022/5/9
 */
export class ValidateSigning extends OhosHapTask {
  private static executed = false;

  constructor(taskService: TargetTaskService) {
    super(taskService, CommonTask.VALIDATE_SIGNING);
  }

  initTaskDepends(): void {
    this.declareDepends(LegacyPreBuild.name);
  }

  protected doTaskAction(): void {
    if (ValidateSigning.executed) {
      return;
    }
    const storeFile = KeyStoreHelper.getDefaultDebugKeyStoreLocation();
    if (storeFile !== undefined && !fse.existsSync(storeFile)) {
      KeyStoreHelper.createDefaultDebugStore();
    }
    ValidateSigning.executed = true;
  }
}
